package LeetCode.month02.day37;

import java.util.ArrayList;
import java.util.List;

public class No06 {
    public static void main(String[] args) {
        System.out.println(convert("PAYPALISHIRING",4));
    }
    public static String convert(String s, int numRows) {
        if(numRows==1) return s;
        int n = s.length();
        int flag = 1;
        int index= 0;
        List<List<Character>> lists = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            lists.add(new ArrayList<>());
        }
        for (int i = 0; i < n; i++) {
            lists.get(index).add(s.charAt(i));
            if(index==0){
                flag=1;
            }else if(index==numRows-1){
                flag=-1;
            }
            index+=flag;
        }
        StringBuilder sb = new StringBuilder();
        for (List<Character> list : lists) {
            for (Character c : list) {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}
